package com.wnxy.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.common.util.GaoDeMapUtil;
import com.wnxy.admin.dto.SelectDto;
import com.wnxy.admin.entity.Region;
import com.wnxy.admin.service.IRegionService;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.dto.JChargingStationDto;
import org.springframework.web.bind.annotation.*;


import java.util.Arrays;
import java.util.List;

import java.util.Map;
import java.util.stream.Collectors;

import com.wnxy.admin.entity.ChargingStation;
import com.wnxy.admin.service.IChargingStationService;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 充电站 前端控制器
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@RestController
@CrossOrigin
@RequestMapping("/charging-station")
public class ChargingStationController {

    private final IChargingStationService chargingStationServiceImpl;
    private final IRegionService regionServiceImpl;

    public ChargingStationController(IChargingStationService chargingStationServiceImpl, IRegionService regionServiceImpl) {
        this.chargingStationServiceImpl = chargingStationServiceImpl;
        this.regionServiceImpl = regionServiceImpl;
    }

    @GetMapping("/all")
    public ResponseResult<List<ChargingStation>> all(){
        List<ChargingStation> list = chargingStationServiceImpl.list();
        return ResponseResult.ok(list);
    }

    @PostMapping("/findByPage")
    public ResponseResult<PageInfo<JChargingStationDto>> findByPage(@RequestBody Map<String,Object> params){
        String chargingStationName = (String) params.get("chargingStationName");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //2.调用业务层
        PageInfo<JChargingStationDto> pageInfo = chargingStationServiceImpl.findByPage(chargingStationName, pageNum, pageSize);
        //3.返回结果
        return ResponseResult.ok(pageInfo);
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody ChargingStation chargingStation){
        String lonLat = GaoDeMapUtil.getLonLat(chargingStation.getChargingStationPlace());
        chargingStation.setCoordinate(lonLat);
        chargingStation.setChargingStationMax(60);
        chargingStation.setChargingStationState(1);
        chargingStationServiceImpl.save(chargingStation);
        return ResponseResult.ok();
    }

    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody ChargingStation chargingStation){
        String lonLat = GaoDeMapUtil.getLonLat(chargingStation.getChargingStationPlace());
        chargingStation.setCoordinate(lonLat);
        boolean b = chargingStationServiceImpl.updateById(chargingStation);
        return ResponseResult.ok(b);
    }

    @GetMapping("/delete")
    public ResponseResult delete(Long id){
        chargingStationServiceImpl.removeById(id);
        return ResponseResult.ok();
    }

    @PostMapping("/deleteBatch")
    public ResponseResult deleteBatch(@RequestBody String[] ids){
        chargingStationServiceImpl.removeBatchByIds(Arrays.asList(ids));
        return ResponseResult.ok();
    }

    @PostMapping("/addAddress")
    public ResponseResult addAddress(@RequestBody ChargingStation chargingStation){
        chargingStationServiceImpl.addAddress(chargingStation);
        return ResponseResult.ok();
    }

    //查找地址
    @GetMapping("/getAllProvinces")
    public ResponseResult getAllProvinces(){
        List<Region> list = regionServiceImpl.list(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getLevel, 1));
        List<SelectDto> collect = list.stream().map(e -> {
            return new SelectDto(e.getId(), e.getName());
        }).collect(Collectors.toList());
        return ResponseResult.ok(collect);
    }

    @GetMapping("/findByProvince")
    public ResponseResult findByProvince(Long provinceId){
        List<Region> cities = regionServiceImpl.list(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getPid, provinceId));
        List<SelectDto> collect = cities.stream().map(e -> {
            return new SelectDto(e.getId(), e.getName());
        }).collect(Collectors.toList());
        return ResponseResult.ok(collect);
    }

    @GetMapping("/findByCity")
    public ResponseResult findByCity(Long cityId){
        List<Region> areas = regionServiceImpl.list(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getPid, cityId));
        List<SelectDto> collect = areas.stream().map(e -> {
            return new SelectDto(e.getId(), e.getName());
        }).collect(Collectors.toList());
        return ResponseResult.ok(collect);
    }

    //判断充电站编号是否存在
    @GetMapping("/isNumbersExists")
    public ResponseResult isNumbersExists(String number){
        boolean result = chargingStationServiceImpl.findByNumbers(number) != null;
        return ResponseResult.ok(result);
    }

}
